package com.hxh.generater.vue.springboot3_vue3.gen;

import com.hxh.entity.Project;
import com.hxh.entity.TemplateConfig;
import com.hxh.entity.po.ClassInfo;
import com.hxh.generater.common.remote.VueRemoteUtil;
import com.hxh.util.GenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;

@Slf4j
public class GenSb3Vue3ViewRemote {

    private static String generationAdd(Project project, ClassInfo classInfo, boolean writeFile) throws IOException {
        if (!VueRemoteUtil.checkNeedProcessAddMethod(classInfo.getProjectTablePageConfig())) {
            return null;
        }
        String entityName = StringUtils.uncapitalize(classInfo.getClassName());
        VueRemoteUtil vueRemoteUtil = new VueRemoteUtil();
        vueRemoteUtil.setProject(project);
        vueRemoteUtil.setClassInfo(classInfo);
        vueRemoteUtil.setWriteFile(writeFile);
        vueRemoteUtil.setRemoteUrlSuffix("vue/add");
        String packageMutilDatasourceStr = GenUtil.getMutilDatasourceStrByConfig(project, classInfo, null, File.separator);
        vueRemoteUtil.setWriteFilePath("src\\views\\" + packageMutilDatasourceStr + entityName + "\\config\\");
        vueRemoteUtil.setWriteFileName("add.ts");
        return vueRemoteUtil.process();
    }

    public static String generationAdd(Project project, ClassInfo classInfo) throws IOException {
        return generationAdd(project, classInfo, true);
    }

    public static String generationAdd(ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        Project project = new Project();
        project.setTemplateConfig(templateConfig);
        return generationAdd(project, classInfo, false);
    }

    private static String generationSearch(Project project, ClassInfo classInfo, boolean writeFile) throws IOException {
        String entityName = StringUtils.uncapitalize(classInfo.getClassName());

        VueRemoteUtil vueRemoteUtil = new VueRemoteUtil();
        vueRemoteUtil.setProject(project);
        vueRemoteUtil.setClassInfo(classInfo);
        vueRemoteUtil.setWriteFile(writeFile);
        vueRemoteUtil.setRemoteUrlSuffix("vue/search");
        String packageMutilDatasourceStr = GenUtil.getMutilDatasourceStrByConfig(project, classInfo, null, File.separator);
        vueRemoteUtil.setWriteFilePath("src\\views\\" + packageMutilDatasourceStr + entityName + "\\config\\");
        vueRemoteUtil.setWriteFileName("search.ts");
        return vueRemoteUtil.process();
    }

    public static String generationSearch(Project project, ClassInfo classInfo) throws IOException {
        return generationSearch(project, classInfo, true);
    }

    public static String generationSearch(ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        Project project = new Project();
        project.setTemplateConfig(templateConfig);
        return generationSearch(project, classInfo, false);
    }

    public static String generationEdit(Project project, ClassInfo classInfo) throws IOException {
        return generationEdit(project, classInfo, true);
    }

    public static String generationEdit(ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        Project project = new Project();
        project.setTemplateConfig(templateConfig);
        return generationEdit(project, classInfo, false);
    }

    private static String generationEdit(Project project, ClassInfo classInfo, boolean writeFile) throws IOException {
       if (!VueRemoteUtil.checkNeedProcessEditMethod(classInfo.getProjectTablePageConfig())){
           return null;
       }
        String entityName = StringUtils.uncapitalize(classInfo.getClassName());

        VueRemoteUtil vueRemoteUtil = new VueRemoteUtil();
        vueRemoteUtil.setProject(project);
        vueRemoteUtil.setClassInfo(classInfo);
        vueRemoteUtil.setWriteFile(writeFile);
        vueRemoteUtil.setRemoteUrlSuffix("vue/edit");
        String packageMutilDatasourceStr = GenUtil.getMutilDatasourceStrByConfig(project, classInfo, null, File.separator);
        vueRemoteUtil.setWriteFilePath("src\\views\\" + packageMutilDatasourceStr + entityName + "\\config\\");
        vueRemoteUtil.setWriteFileName("edit.ts");
        return vueRemoteUtil.process();
    }

    public static String generationContent(Project project, ClassInfo classInfo) throws Exception {
        return generationContent(project, classInfo, true);
    }

    public static String generationContent(ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        Project project = new Project();
        project.setTemplateConfig(templateConfig);
        return generationContent(project, classInfo, false);
    }

    private static String generationContent(Project project, ClassInfo classInfo, boolean writeFile) throws IOException {
        String entityName = StringUtils.uncapitalize(classInfo.getClassName());

        VueRemoteUtil vueRemoteUtil = new VueRemoteUtil();
        vueRemoteUtil.setProject(project);
        vueRemoteUtil.setClassInfo(classInfo);
        vueRemoteUtil.setWriteFile(writeFile);
        vueRemoteUtil.setRemoteUrlSuffix("vue/content");
        String packageMutilDatasourceStr = GenUtil.getMutilDatasourceStrByConfig(project, classInfo, null, File.separator);
        vueRemoteUtil.setWriteFilePath("src\\views\\" + packageMutilDatasourceStr + entityName + "\\config\\");
        vueRemoteUtil.setWriteFileName("content.ts");
        return vueRemoteUtil.process();
    }

    public static String generationIndexVue(Project project, ClassInfo classInfo) throws IOException {
        return generationIndexVue(project, classInfo, true);
    }

    public static String generationIndexVue(ClassInfo classInfo, TemplateConfig templateConfig) throws IOException {
        Project project = new Project();
        project.setTemplateConfig(templateConfig);
        return generationIndexVue(project, classInfo, false);
    }

    private static String generationIndexVue(Project project, ClassInfo classInfo, boolean writeFile) throws IOException {
        String entityName = StringUtils.uncapitalize(classInfo.getClassName());

        VueRemoteUtil vueRemoteUtil = new VueRemoteUtil();
        vueRemoteUtil.setProject(project);
        vueRemoteUtil.setClassInfo(classInfo);
        vueRemoteUtil.setWriteFile(writeFile);
        vueRemoteUtil.setRemoteUrlSuffix("vue/indexVue");
        String packageMutilDatasourceStr = GenUtil.getMutilDatasourceStrByConfig(project, classInfo, null, File.separator);
        vueRemoteUtil.setWriteFilePath("src\\views\\" + packageMutilDatasourceStr + entityName + "\\");
        vueRemoteUtil.setWriteFileName("index.vue");
        return vueRemoteUtil.process();

    }


}
